import { HTTP } from "../../utils/http"
import api from '../../utils/api.js'
import { config } from '../../utils/config'
import common from '../../utils/common'
let http = new HTTP()
Page({
    data: {
        date: common.date.format_month,
        dotMonth: common.date.format_dot_month,
        endMonth: common.date.format_month,
        itemimg: config.imgUrl + '/images/my/s6.png',
        imgUrl: config.imgUrl,
        page: 1,
        list: [],
        user_token: wx.getStorageSync('token'),
    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        let format_month = e.detail.value;
        this.setData({
            page: 1,
            list: [],
            dotMonth: format_month.replace('-', '.'),
        });
        this.queryJifen(format_month)
    },
    onLoad: function () {
        this.queryJifen(this.data.endMonth)
        this.getCoins()
    },
    queryJifen(format_month) {
        let that = this
        http.request({
            url: api.accountPoints,
            data: {
                token: this.data.user_token,
                month: format_month,
                page: that.data.page
            },
            success: (res) => {
                console.log(res.data.list)
                this.setData({
                    list: that.data.list.concat(res.data.list),
                    date: format_month,
                    totalPage: Math.ceil(res.data.total / res.data.per_page)
                })
            }
        })
    },
    toDisable() {
        wx.navigateTo({
            url: '/pages/getJiFen/disable/disable'
        })
    },

    //获取积分
    getCoins() {
        let _that = this;
        http.request({
            url: api.accountCoins,
            data: {
                token: _that.data.user_token,
            },
            success: (res) => {
                console.log(res)
                if (res.code == 200) {
                    this.setData({
                        jifen_num: res.data.points,
                        frozen_points: res.data.frozen_points
                    })
                }
            }
        })
    },
    onReachBottom: function () {
        console.log('触底刷新');
        if (this.data.page < this.data.totalPage) {
            this.setData({
                page: this.data.page + 1
            })
            let format_month = this.data.date
            this.queryJifen(format_month)
        }
    },
})